﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TMDT.BusinessLogic;
using TMDT.BusinessObjects;
using TMDT.Web.UI.Commons;

namespace TMDT.Web.UI.Controls
{
    public partial class DangSanPhamControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            LoadChungLoai();
            LoadLoaiSanPham();
            LoadNhaSanXuat();
            
            LoadRepeater(ddlLoaiSanPham.SelectedValue);
        }

        private void LoadChungLoai()
        {
            var service = new ChungLoaiSanPhamManager();
            ddlChungLoai.LoadDataSource(service.GetChungLoaiSanPham(), "TenChungLoai", "IDChungLoai");
        }

        private void LoadNhaSanXuat()
        {
            var service = new NhaSanXuatManager();
            ddlNhaSanXuat.LoadDataSource(service.GetNhaSanXuat(), "TenNhaSanXuat", "IDNhaSanXuat");
        }

        private void LoadLoaiSanPham()
        {
            var service = new LoaiSanPhamManager();
            ddlLoaiSanPham.LoadDataSource(service.GetLoaiSanPhamByChungLoai(ddlChungLoai.SelectedValue), "TenLoaiSanPham", "IDLoaiSanPham");
        }

        protected void ddlIDLoaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            var idLoaiSanPham = ddlLoaiSanPham.SelectedValue;

            LoadRepeater(idLoaiSanPham);
        }

        protected void LoadRepeater(object idLoaiSanPham)
        {
            var listChiTietThuocTinh = new ChiTietThuocTinhManager().GetChiTietThuocTinhByIDLoai(idLoaiSanPham);
            rptNoiDungThuocTinh.DataSource = listChiTietThuocTinh;
            rptNoiDungThuocTinh.DataBind();

        }

        protected void rptNoiDungThuocTinh_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lblTenThuocTinh = e.Item.FindControl("lblTenThuocTinh") as Label;

            var chiTietThuocTinh = e.Item.DataItem as ChiTietThuocTinh;
            if (chiTietThuocTinh == null)
                return;
            var thuocTinh = new ThuocTinhManager().GetThuocTinh(chiTietThuocTinh.IDThuocTinh);

            lblTenThuocTinh.Text = thuocTinh.TenThuocTinh;
        }

        protected void lbnDangSanPham_Click(object sender, EventArgs e)
        {
            Guid g = Guid.NewGuid();
            string strFileName="";
            if (fuHinhAnh.HasFile)
            {
                string strP = "~/Upload/" + g + fuHinhAnh.FileName;
                strFileName = "/../Upload/" + g + fuHinhAnh.FileName;
                fuHinhAnh.SaveAs(Server.MapPath(strP));
            }
            else
            {
                strFileName = "/../Images/noimage.gif";
            }
            var sanPham = new BusinessObjects.SanPham
                              {
                                  IDSanPham = Guid.NewGuid(),
                                  IDLoaiSanPham = new Guid(ddlLoaiSanPham.SelectedValue),
                                  IDNguoiBan = new TaiKhoanManager().GetIDByTenDangNhap(Session["User"].ToString()).IDTaiKhoan,
                                  IDNhaSanXuat = new Guid(ddlNhaSanXuat.SelectedValue),
                                  IsAn = true,
                                  IsKhuyenMai  = chkIsKhuyenMai.Checked,
                                  NoiDungKhuyenMai = txtNoiDungKhuyenMai.Text,
                                  NgayTao = DateTime.Now,
                                  SoLuotXem = 0,
                                  SoLuong = int.Parse(txtSoLuong.Text),
                                  ThoiGianBaoHanh = int.Parse(txtThoiGianBaoHanh.Text),
                                  Gia = int.Parse(txtGia.Text),
                                  TenSanPham = txtTenSanPham.Text
                              };
            var service = new SanPhamManager();
            service.Add(sanPham);

            var hinhAnh = new HinhAnhManager();
            var entity = new HinhAnh
                             {
                                 IDHinhAnh = Guid.NewGuid(),
                                 IDSanPham = sanPham.IDSanPham,
                                 URL = strFileName
                             };
            hinhAnh.Add(entity);

            for (int i = 0; i < rptNoiDungThuocTinh.Items.Count; i++)
            {
                var rowNoiDungThuocTinh = rptNoiDungThuocTinh.Items[i];
                var txtNoiDung = rowNoiDungThuocTinh.FindControl("txtNoiDungThuocTinh") as TextBox;
                var hfdNoiDungThuocTinh = rowNoiDungThuocTinh.FindControl("hfdNoiDungThuocTinh") as HiddenField;

                var noiDungSanPham = new NoiDungSanPham
                                         {
                                             IDSanPham = sanPham.IDSanPham,
                                             IDThuocTinh = new Guid(hfdNoiDungThuocTinh.Value),
                                             NoiDung = txtNoiDung.Text
                                         };
                new NoiDungSanPhamManager().Add(noiDungSanPham);
            }

            Response.Redirect("SanPham.aspx");
        }

        protected void ddlChungLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadLoaiSanPham();
            LoadRepeater(ddlLoaiSanPham.SelectedValue);
        }
    }
}